package com.conversationboard.view;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.conversationboard.config.Configuration;
import com.conversationboard.formatter.DateFormatter;
import com.conversationboard.formatter.ImageHider;
import com.conversationboard.model.Message;
import com.conversationboard.model.PollItem;
import com.conversationboard.model.Thread;
import com.conversationboard.model.User;

/**
 * Class to display threads that are polls
 * 
 * @author Keith Watson
 */

public class PollDisplay {

	public static String displayClosingDate(Thread thread, User user) {
		
		String dateDifference = DateFormatter.prettyPrintFutureTime(thread.getPollClosingDate(), new Date(), user.getPreferences().getTimeZone());
		return "<p><strong>Poll Closing Date: " + DateFormatter.prettyPrint(thread.getPollClosingDate(), user) + " (in " + dateDifference + ")</strong></p>";
	}


	/**
	 * If the poll is locked, display the options, but not the radio buttons.
	 * 
	 * @param thread
	 * @param user
	 * @return
	 * @throws SQLException
	 */

	public static String displayLocked(String key, Thread thread, User user, boolean mobileDevice) throws SQLException {

		if (!thread.isPoll()) {
			return "";
		}

		StringBuilder buffer = new StringBuilder();

		buffer.append("<div class=\"poll\">");

		Message message = thread.getFirstMessage();

		buffer.append(message.getBody());

		List<PollItem> options = thread.getPollItems();

		buffer.append("<ul>");

		for (PollItem pollItem : options) {
			buffer.append("<li>" + pollItem.getOptionText() + "</li>");
		}

		buffer.append("</ul>");

		MessagesDisplay.appendUserDetails(key, buffer, thread, message, user, mobileDevice);

		if (user.isAdmin()) {
			buffer.append("<p>");
			MessageAdminLinksDisplay.includeAdminLinks(buffer, thread.getBoardId(), thread.getThreadId(), 1, true, thread.getLoginId(), thread.getIpAddress(), user);
			buffer.append("</p>");
		}

		buffer.append("</div>");

		String body = buffer.toString();

		/* Get rid of any images, if you've set that preference */

		if ((!user.getPreferences().isShowImages())) {
			body = ImageHider.substituteImagesForLinks(body, user.isAdmin(), false, (!user.getPreferences().isShowImages()));
		}

		return body;
	}


	public static String display(String key, Thread thread, User user, boolean mobileDevice) throws SQLException {

		if (!thread.isPoll()) {
			return "";
		}

		StringBuilder buffer = new StringBuilder();

		buffer.append("<div class=\"formsectionlight\">");

		buffer.append("<form action=\"" + Configuration.getInstance().getRoot() + "/VoteControllerServlet\" method=\"post\">");

		buffer.append("<input name=\"boardId\" value=\"" + thread.getBoardId() + "\" type=\"hidden\" />");
		buffer.append("<input name=\"threadId\" value=\"" + thread.getThreadId() + "\" type=\"hidden\" />");

		Message message = thread.getFirstMessage();

		buffer.append(message.getBody());

		List<PollItem> options = thread.getPollItems();

		for (PollItem pollItem : options) {
			buffer.append("<input type=\"radio\" name=\"optionid\" value=\"" + pollItem.getOptionId() + "\">" + pollItem.getOptionText().trim() + "<br />");
		}

		buffer.append("<br />");
		buffer.append("<input type=\"submit\" value=\"Vote\" /> ");
		buffer.append("<br />");
		buffer.append("</div>");

		MessagesDisplay.appendUserDetails(key, buffer, thread, message, user, mobileDevice);

		if (user.isAdmin()) {
			buffer.append("<p>");
			MessageAdminLinksDisplay.includeAdminLinks(buffer, thread.getBoardId(), thread.getThreadId(), 1, true, thread.getLoginId(), thread.getIpAddress(), user);
			buffer.append("</p>");
		}

		buffer.append("</form>");

		String body = buffer.toString();
		body = ImageHider.substituteImagesForLinks(body, user.isAdmin(), false, (!user.getPreferences().isShowImages()));

		return body;

	}

}
